class UsersController < ApplicationController

  # 似乎是应该选出winstreak#streak_count最大的几个winstreak
  def index
    @filter = params[:filter] || "current"
    case @filter
    when "current"
      # @winstreaks = Winstreak.scoped(:include=>:user,:order=>"#{User.table_name}.win_percentage DESC")
      @users = User.scoped(:include=>:winstreak,:order=>"#{Winstreak.table_name}.streak_count DESC")
      # @winstreaks = @users.collect &:winstreak
      @action = "current_streak"
    when "total_bets"
      @users = User.scoped(:order=>"#{User.table_name}.bets_count DESC")
      @action = "bets"
    when "percentage"
      @users = User.scoped(:order=>"#{User.table_name}.win_percentage DESC")
      @action = "users_win_percentage"
    when "all"
      # best streak
      @winstreaks = Winstreak.scoped(:order=>"streak_count DESC")
      @streak_name = "Best Streak"
    when "recent_winner"
      # @bets = Bet.scoped(:conditions=>{:win=>true},:group=>"#{Bet.table_name}.user_id",:order=>"#{Bet.table_name}.updated_at")
      @winstreaks = Winstreak.scoped(:conditions=>{:got_prize=>true})
      @action = "recent_winners"
    else
      @winstreaks = Winstreak.scoped(:order=>"streak_count DESC")
    end

    common_scope  = {:limit=>20}
    friends_scope = {:conditions=>["user_id in (?)",current_user.friend_ids.push(current_user.id)]}
    case params[:type]
    when "friends","",nil
      @winstreaks = @winstreaks.scoped(friends_scope.merge(:group=>"#{Winstreak.table_name}.user_id")) if @winstreaks
      @users      = @users.scoped(:conditions=>["#{User.table_name}.id in (?)",current_user.friend_ids.push(current_user.id)]) if @users
      @bets       = @bets.scoped(friends_scope) if @bets
    when "global"
      # default scope was global
    end
    @winstreaks = @winstreaks.all common_scope.merge(:select=>"*,max(streak_count) AS streak_count",:group=>"#{Winstreak.table_name}.user_id") if @winstreaks
    @users      = @users.all common_scope if @users
    @bets       = @bets.all common_scope if @bets
    logger.debug "action: #{@action}"
    render :action=>@action if @action
  end
  def hide_bookmark
  logger.error "----------------------------------"
  logger.error params[:id]
  logger.error "----------------------------------"
  user = User.find(params[:id])
  user.hide_bookmark = true
  user.save
  render :nothing=>true
  end

  def hide_howto
  user = User.find(params[:id])
  user.hide_howto = true
  user.save
  render :nothing=>true
  end

  def show
  end

  def invited
    i = 0
    ids = params[:ids]
    ids.each do |id| 
      unless PointHistory.find(:first,:conditions=>["user_id =? and action='invite friend' and related_user_id=?",current_user.id,id])
        PointHistory.create(:user_id=>current_user.id,:action=>'invite friend',:point=>PointConfig.find_by_name("invite_sent").point,:earning => true,:related_user_id=>id)
	i = i + 1
      end
    end
    current_user.point = current_user.point + i*PointConfig.find_by_name("invite_sent").point
    current_user.save
    flash[:notice] = "You earned #{i*PointConfig.find_by_name("invite_sent").point} WinPoints via your invite request(s)."
    redirect_to buy_wins_path
  end

  def refresh_profile
    @user = User.find params[:id]
    WinstreakPublisher.deliver_update_profile(@user)
    flash[:notice] = "Profile refreshed."
    redirect_to bets_path
  end

end
